﻿using System;
using System.Windows;

using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using ZhiHeng.OneCard.WpfClient.ViewModel;
namespace ZhiHeng.OneCard.WpfClient
{
    /// <summary>
    /// Interaction logic for Main.xaml
    /// </summary>
    public partial class Main
    {
        public static bool MaxSta = false;
        public Main()
        {
            InitializeComponent(); 
            //InitMetroItems();

        }
        protected override void OnPreviewKeyDown(System.Windows.Input.KeyEventArgs e)
        {
            base.OnPreviewKeyDown(e);
            if (e.Key ==Key.Escape)
            {
                var vm = (MainViewModel)MainGrid.DataContext;
                vm.ReturnToStart();
                //e.Handled = true;
            }
            if (e.Key == Key.F11)
            {
                ChangeWindowState();
            }
        }

        private void ChangeWindowState()
        {
            if (this.WindowState == WindowState.Maximized)
            {
                MaxSta = false;
                this.WindowState = WindowState.Normal;
            } 
            else
            {
                MaxSta = true;
                this.WindowState = WindowState.Maximized;
            } 
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.MaxWidth = Screen.PrimaryScreen.WorkingArea.Width;
            this.MaxHeight = Screen.PrimaryScreen.WorkingArea.Height;
        }

        private void grid1_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
                DragMove();
            else
                return;
        }
        #region 鼠标滑动事件
        private void image1_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            try
            {
                BitmapImage image = new BitmapImage(new Uri(@"\Icon\Maxmize\minimize-mousedown.png", UriKind.Relative));
                image1.Source = image;
            }
            catch (Exception ex)
            {

            }
        }
        private void image1_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            try
            {
                BitmapImage image = new BitmapImage(new Uri(@"\Icon\Maxmize\minimize.png", UriKind.Relative));
                image1.Source = image;

            }
            catch (Exception ex)
            {

            }
        }
        private void image2_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            try
            {
                BitmapImage image = MaxSta == true ? new BitmapImage(new Uri(@"\Icon\Maxmize\maxmize-mousedown.png", UriKind.Relative)) : new BitmapImage(new Uri(@"\Icon\Maxmize\restore-mousedown.png", UriKind.Relative));
                image2.Source = image;
            }
            catch (Exception ex)
            {

            }
        }

        private void image2_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            try
            {
                BitmapImage image = MaxSta == true ? new BitmapImage(new Uri(@"\Icon\Maxmize\maxmize.png", UriKind.Relative)) : new BitmapImage(new Uri(@"\Icon\Maxmize\restore.png", UriKind.Relative));
                image2.Source = image;
            }
            catch (Exception ex)
            {

            }
        }

        private void image3_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            try
            {
                BitmapImage image = new BitmapImage(new Uri(@"\Icon\Maxmize\close-mousedown.png", UriKind.Relative));
                image3.Source = image;
            }
            catch (Exception ex)
            {

            }
        }

        private void image3_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            try
            {
                BitmapImage image = new BitmapImage(new Uri(@"\Icon\Maxmize\close.png", UriKind.Relative));
                image3.Source = image;
            }
            catch (Exception ex)
            {

            }
        }

        #endregion
        #region 点击事件
        private void image1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        private void image2_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (this.WindowState == WindowState.Maximized)
            {
                MaxSta = false;
                this.WindowState = WindowState.Normal;
            }
            else
            {
                MaxSta = true;
                this.WindowState = WindowState.Maximized;

            }
        }

        private void image3_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Close();
            System.Environment.Exit(System.Environment.ExitCode);
        }

        #endregion


        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            //解除动画
            LoginPage.BeginAnimation(UIElement.OpacityProperty, null);
            LoginPage.BeginAnimation(UIElement.VisibilityProperty,null);
            LoginPage.Opacity = 1;
            LoginPage.Visibility = Visibility.Visible;
            LoginPage.LoginStatus = false;
        } 
        private void grid1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount==2)
            {
                ChangeWindowState();
            }
        }

        //private void InitMetroItems()
        //{
        //    var page1 = new StartPageViewModel();
        //    foreach (var menu in FormUtility.GetMenus())
        //    {
        //        var metroTile = new MetroTile();
        //        metroTile.Content = menu.Name;

        //        metroTile.TileColor = (MetroTileColor)menu.TileColor;

        //    }


        //}
         
    }
}
